package com.wuxq.serialport;

import android.util.Log;

import java.io.File;
import java.io.IOException;

import android_serialport_api.SerialPort;
import android_serialport_api.SerialPortFinder;

public class SerialPortTools {

    private SerialPort mSerialPort = null;
    public SerialPortFinder finder = new SerialPortFinder();

    public SerialPortTools(){

    }

    public SerialPort getSerialPort(String path,int baudRate)  {
        return getSerialPort(path,baudRate,0);
    }

    public SerialPort getSerialPort(String path,int baudRate,int flag)  {
        if (mSerialPort == null) {
            try {
                mSerialPort = new SerialPort(new File(path), baudRate, flag);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return mSerialPort;
    }

    public void getAllDevices(){
        String[] entries = finder.getAllDevices();
        String[] entryValues = finder.getAllDevicesPath();
        Log.d("wuxq","串口数量:"+entries.length);
        for (String entryValue : entryValues) {
            Log.d("wuxq","串口地址:"+entryValue);
        }
    }

    public void closeSerialPort() {
        if (mSerialPort != null) {
            mSerialPort.close();
            mSerialPort = null;
        }
    }

    public SerialPortFinder getFinder() {
        return finder;
    }


}
